home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Presentations / Presentations ’96 / Papers ’96 / Book of Practical Objects / BasicObjects ƒ / CFIFOQueue.h < prev    next >
Encoding:
Text File  |  1996-06-22  |  1.9 KB  |  54 lines  |  [TEXT/CWIE]

  1. // CQueue - a small utility class to allow queuing and dequeuing of objects
  2. //    Add items to the end of the queue, and remove them from the front, one at a time
  3. //    This is a generic queue that can be used without overriding for any reason.
  4. //    Simply tell the object how big the elements etc are, and then feed it pointers
  5. //    to your data.
  6.  
  7. #ifndef    __CFIFOQUEUE__
  8. #define    __CFIFOQUEUE__
  9.  
  10.  
  11. #pragma    once
  12.  
  13. enum {
  14.     kDefaultQueueSize = 10,
  15.     
  16.     kQueueAlreadyTooBig = 1            // This error says you can't shrink the queue size smaller
  17.                                     // You already have more elements than that in the queue
  18. };
  19.  
  20.  
  21. class    CFIFOQueue
  22. {
  23.     Handle    fQueueHead;
  24.     long    fQueueMaxElements;        // This determines how much memory is allocated for the queue
  25.     long    fQueueElements;            // How many elements we have in the queue currently
  26.     long    fQueueElementSize;        // How big each element in the queue is
  27.     Boolean    fOnlyLongs;                // This flag means we are cheating and taking the void* from
  28.                                     // AddElement as a long, not a pointer to data. This can make
  29.                                     // us MUCH faster for pointers, etc.
  30.     Boolean    fAutoExpand;            // Can we grow the queue on need, or only when told?
  31.     
  32. public:
  33.             CFIFOQueue(Boolean autoExpand = true, Boolean onlyLongs = false);
  34.             CFIFOQueue(long elementSize, long maxElements = kDefaultQueueSize,
  35.                     Boolean autoExpand = true, Boolean onlyLongs = false);
  36.     virtual    ~CFIFOQueue(void);
  37.         
  38.     OSErr    SetMaxQueueSize(long maxElements);    // Could return an error (-108, or QueueTooBig)
  39.     long    GetMaxQueueSize(void);
  40.     
  41.     long    GetQueueElementSize(void);
  42.  
  43.     // these functions return FALSE if they failed in some way. GetFrontElement could
  44.     // fail if the queue is empty.
  45.     virtual    Boolean    AddElement(void *theData);            // The address to take data from
  46.     virtual    Boolean    GetFrontElement(void* theData);
  47.     
  48.     long    GetQueueLength(void);
  49.  
  50. protected:
  51.     virtual    void    AddOneElementToQueue(void *theData);        // Add without bounds check
  52. };
  53.  
  54. #endif